//
//  NSString+ExChineseName.m
//  OC-Example
//
//  Created by lcj on 2020/11/3.
//  Copyright © 2020 apang.men. All rights reserved.
//

#import "NSString+ExChineseName.h"

@implementation NSString (ExChineseName)

#pragma mark - 明确列出转换平台翻译错误的多音字姓氏，并列出正确匹配项
/**
 *  所有的多音字匹配的拼音键值对，每个姓氏多音字对应一个数组，数组中第一个字为正确发音，第二个为错误发音，错误发音用来做字符串替换使用
 *  多音字姓氏使用系统默认转换时，如果转换错误，将使用该键值对匹配替换
 */
+ (NSDictionary*)getDicForAllPolyphoneSurnamePinYin;
{
    static NSDictionary* g_pAllPolyphoneSurnamePinYin = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^
                  {
                      g_pAllPolyphoneSurnamePinYin = [NSMutableDictionary dictionaryWithObjectsAndKeys
                                                      :@[@"po",@"fan"],@"繁"
                                                      ,@[@"ou",@"qu"],@"区"
                                                      ,@[@"qiu",@"chou"],@"仇"
                                                      ,@[@"chong",@"zhong"],@"种"
                                                      ,@[@"shan",@"dan"],@"单"
                                                      ,@[@"xie",@"jie"],@"解"
                                                      ,@[@"zha",@"cha"],@"查"
                                                      ,@[@"zeng",@"ceng"],@"曾"
                                                      ,@[@"bi",@"mi"],@"秘"
                                                      ,@[@"yue",@"le"],@"乐"
                                                      ,@[@"chong",@"zhong"],@"重"
                                                      ,@[@"piao",@"pu"],@"朴"
                                                      ,@[@"miao",@"mou"],@"缪"
                                                      ,@[@"zhai",@"di"],@"翟"
                                                      ,@[@"she",@"zhe"],@"折"
                                                      ,@[@"he",@"hei"],@"黑"
                                                      ,@[@"ge",@"gai"],@"盖"
                                                      ,@[@"shen",@"chen"],@"沈"
                                                      ,@[@"yu chi",@"wei chi"],@"尉迟"
                                                      ,@[@"mo qi",@"wan qi"],@"万俟"
                                                      ,nil];
                  });
    return g_pAllPolyphoneSurnamePinYin;
}

- (NSString *)ap_ChineseNameConvertToLatin {
    NSString * convertStr = self;
    [convertStr stringByReplacingOccurrencesOfString:@"·" withString:@""];
    NSMutableString *mutableString = [NSMutableString stringWithString:convertStr];
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    NSString *pinyinString = [mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    NSMutableString* resultString = [NSMutableString stringWithString:pinyinString];
    NSArray* allPolyphoneSurname = [[NSString getDicForAllPolyphoneSurnamePinYin] allKeys];
    for (NSString* tempPolyphoneSurname in allPolyphoneSurname)
    {
        if ([convertStr hasPrefix:tempPolyphoneSurname])
        {
            NSArray* arrayOfPolyphoneSurnamePinYin = [[NSString getDicForAllPolyphoneSurnamePinYin] valueForKey:tempPolyphoneSurname];
            NSString* rightPinYin = [arrayOfPolyphoneSurnamePinYin objectAtIndex:0];
            NSString* wrongPinYin = [arrayOfPolyphoneSurnamePinYin objectAtIndex:1];
            NSRange rangeOfPinYin = NSMakeRange(0, wrongPinYin.length);
            if ([[resultString substringWithRange:rangeOfPinYin] isEqualToString:wrongPinYin])
            {
                [resultString replaceCharactersInRange:rangeOfPinYin withString:rightPinYin];
            }
            break;
        }
    }
    return resultString;
}
@end
